<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/6/11
 * Time: 11:10
 */

namespace app\commi\controller;

use think\Db;
use think\Request;

class Cost extends Base
{
    /**
     * 费用详情列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $match_id = Db::table('up_match')
                ->where('match_committee_id', session('com_id'))
                ->where('match_member_id', session('cm_id'))
                ->value('match_id');
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('match_cost_info')
                ->join('up_match_cost', 'mi_cost_id = mc_id', 'left')
                ->where('mi_del', 1)
                ->where('mi_match_id', $match_id)
                ->count();// 获取总条数
            $lists = Db::name('match_cost_info')
                ->join('up_match_cost', 'mi_cost_id = mc_id', 'left')
                ->field('mi_id, mc_name, mi_price, mi_must')
                ->where('mi_del', 1)
                ->where('mi_match_id', $match_id)
                ->page($Nowpage,$limits)
                ->order('mi_id desc')
                ->select();
        
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加费用明细
     * @return mixed|\think\response\Json
     */
    public function costAdd(){
        if(Request::instance()->isPost()){
            $match_id = Db::table('up_match')
                ->where('match_committee_id', session('com_id'))
                ->where('match_member_id', session('cm_id'))
                ->value('match_id');
            if(!$match_id){
                return json(['code' => 100, 'msg' => '请先创建赛事']);
            }
            $mi_cost_id = trim(input('mi_cost_id'), ' ');
            $mi_price = trim(input('mi_price'), ' ');
            $mi_must = input('mi_must');
            $exist = Db::table('up_match_cost_info')
                ->where('mi_match_id', $match_id)
                ->where('mi_cost_id', $mi_cost_id)
                ->find();
            if($exist){
                return json(['code' => 100, 'msg' => '重复添加费用']);
            }
            $insert = [
                'mi_cost_id' => $mi_cost_id,
                'mi_match_id' => $match_id,
                'mi_price' => $mi_price,
                'mi_must' => $mi_must
            ];
            $res = Db::table('up_match_cost_info')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $lose = $this->lostTime();
        if($lose == 1){
            return $this->fetch('/lose');
        }else{
            $cost = Db::table('up_match_cost')
                ->field('mc_id, mc_name')
                ->select();
            $this->assign([
                'cost' => $cost
            ]);
            return $this->fetch('cost/costadd');
        }
    }
    
    /**
     * 编辑费用
     * @return mixed|\think\response\Json
     */
    public function costEdit(){
        $mi_id = input('mi_id');
        $info = Db::table('up_match_cost_info')
            ->field('mi_cost_id, mi_match_id, mi_price, mi_must')
            ->where('mi_id', $mi_id)
            ->find();
        if(Request::instance()->isPost()){
            $mi_cost_id = trim(input('mi_cost_id'), ' ');
            $mi_price = trim(input('mi_price'), ' ');
            $mi_must = input('mi_must');
            $exist = Db::table('up_match_cost_info')
                ->where('mi_match_id', $info['mi_match_id'])
                ->where('mi_cost_id', $mi_cost_id)
                ->where('mi_id', 'NEQ', $mi_id)
                ->find();
            if($exist){
                return json(['code' => 100, 'msg' => '重复添加费用']);
            }
            $update = [
                'mi_cost_id' => $mi_cost_id,
                'mi_price' => $mi_price,
                'mi_must' => $mi_must
            ];
            $res = Db::table('up_match_cost_info')->where('mi_id', $mi_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        
        $lose = $this->lostTime();
        if($lose == 1){
            return $this->fetch('/lose');
        }else{
            $cost = Db::table('up_match_cost')
                ->field('mc_id, mc_name')
                ->select();
            $this->assign([
                'cost' => $cost,
                'info' => $info,
                'mi_id' => $mi_id
            ]);
            return $this->fetch('cost/costedit');
        }
        
    }
    
    /**
     * 删除明细
     * @return \think\response\Json
     */
    public function costDel(){
        $lose = $this->lostTime();
        if($lose == 1){
            return json(['code' => 100, 'msg' => '账号已过期，非法操作']);
        }
        $mi_id = input('id');
        $res = Db::table('up_match_cost_info')->where('mi_id', $mi_id)->setField('mi_del', 2);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}